/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.diagnostikoak;

import igraf.peritua.BalorazioaEman;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import dBasea.Balorazioak;

public class EremuPrezioPane extends JDialog {
	
	
	private static final long serialVersionUID = 4301086097320786375L;
	private JPanel panel1;
	private JLabel username;
	private JLabel password;
	private JTextField textField;
	private JTextField textField2;
	private Balorazioak balo=new Balorazioak();
	private JPanel panel2;
	private JButton btn1;
	private JButton btn2;
	private JComboBox comboBox;

	public EremuPrezioPane(final BalorazioaEman aitarenPanela) {
		super();

		getContentPane().setLayout(new BorderLayout());
		this.setTitle("Eremua eta balorea");
		this.setModal(true);
		this.setSize(300, 190);
		this.setLocationRelativeTo(aitarenPanela);

		panel1 = new JPanel();
		panel1.setLayout(new GridLayout(5, 1));
		username = new JLabel("Behaketak");
		password = new JLabel("Balorea");
		textField = new JTextField(12);
		textField2 = new JTextField(12);
		
		comboBox = new JComboBox();
		panel1.add(comboBox);
		panel1.add(username);
		panel1.add(textField);
		panel1.add(password);
		panel1.add(textField2);

		btn1 = new JButton("Gehitu");
		btn2 = new JButton("Ezeztatu");
		panel2 = new JPanel(new FlowLayout());
		panel2.add(btn1);
		panel2.add(btn2);
		try {
			boxCarga();
		}
		catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "Ezin izan da kargatu DB-tik eremu zerrenda", "Oker", JOptionPane.ERROR_MESSAGE);				
			e.printStackTrace();
		}

		getContentPane().add(panel1, BorderLayout.CENTER);
		getContentPane().add(panel2, BorderLayout.SOUTH);

		btn1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (zerbaitIdatziDu()) {
					aitarenPanela.addNewRow((String)comboBox.getSelectedItem(),textField.getText(),Float.parseFloat(textField2.getText()));
					setVisible(false);
					dispose();
				}

			}
		});

		btn2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				dispose();
			}
		});

		this.setVisible(true);
	}
	private void boxCarga() throws SQLException {
		List<String> eremuak = balo.listatuEremuak();
		comboBox.removeAllItems();
		if (eremuak != null) {
			for (String hiria: eremuak) {
				comboBox.addItem(hiria);
			}
		}
	}

	public boolean zerbaitIdatziDu() {
		boolean aurki = false;

		aurki = (textField.getText().length() > 0 && textField2.getText()
				.length() > 0);
		return aurki;
	}
	

}
